package com.xxxx.rpc.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.xxxx.rpc.mapper.GoodsCategoryMapper;
import com.xxxx.rpc.pojo.GoodsCategory;
import com.xxxx.rpc.service.GoodsCategoryService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.util.List;

@Service(interfaceClass = GoodsCategoryService.class)
@Component
public class GoodsCategoryServiceImpl implements GoodsCategoryService {

    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Value("${goods.category.list.key}")
    private String goodsCategoryListKey;


    /**
     * 查询级联列表
     *
     * @return
     */
    @Override
    public List<GoodsCategory> selectGoodsCategoryAll() throws Exception {
        ValueOperations<String, String> ops = redisTemplate.opsForValue();
        String gcListJson = ops.get(goodsCategoryListKey);
        //如果redis有数据，使用redis
        if (!StringUtils.isEmpty(gcListJson)){
            List<GoodsCategory> goodsCategoryList = JSON.parseArray(gcListJson,GoodsCategory.class);
            return goodsCategoryList;
        }

        List<GoodsCategory> gcList = goodsCategoryMapper.selectGoodsCategoryAll((short) 0);
        //放入Redis
        ops.set(goodsCategoryListKey,JSON.toJSONString(gcList));
        return gcList;
    }





}


